package com.yst.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.yst.common.annotation.RateLimiter;
import com.yst.common.core.domain.entity.SysUser;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yst.common.annotation.Log;
import com.yst.common.core.controller.BaseController;
import com.yst.common.core.domain.AjaxResult;
import com.yst.common.enums.BusinessType;
import com.yst.system.domain.BasItem;
import com.yst.system.service.IBasItemService;
import com.yst.common.utils.poi.ExcelUtil;
import com.yst.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 产品信息Controller
 *
 * @author hoj
 * @date 2024-05-10
 */
@RestController
@RequestMapping("/system/item")
public class BasItemController extends BaseController {
    @Autowired
    private IBasItemService basItemService;

    /**
     * 查询产品信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:item:list')")
    @GetMapping("/list")
    public TableDataInfo list(BasItem basItem) {
        startPage();
        List<BasItem> list = basItemService.selectBasItemList(basItem);
        return getDataTable(list);
    }

    @Log(title = "产品管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:item:import')")
    @PostMapping("/importData")
    @RateLimiter
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<BasItem> util = new ExcelUtil<BasItem>(BasItem.class);
        List<BasItem> userList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = basItemService.importItem(userList, updateSupport, operName);
        return success(message);
    }

    @Log(title = "产品管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:item:import')")
    @PostMapping("/importImg")
    @RateLimiter
    public AjaxResult importImg(MultipartFile file) {
        return success(basItemService.importImg(file));
    }


    /**
     * 导出模板
     *
     * @param response
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<BasItem> util = new ExcelUtil<BasItem>(BasItem.class);
        util.hideColumn("imgUrl");
        util.importTemplateExcel(response, "产品数据");
    }


    /**
     * 导出产品信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:item:export')")
    @Log(title = "产品信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BasItem basItem) {
        List<BasItem> list = basItemService.selectBasItemList(basItem);
        ExcelUtil<BasItem> util = new ExcelUtil<BasItem>(BasItem.class);
        util.exportExcel(response, list, "产品信息数据");
    }

    /**
     * 获取产品信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:item:query')")
    @GetMapping(value = "/{itemId}")
    public AjaxResult getInfo(@PathVariable("itemId") Long itemId) {
        return success(basItemService.selectBasItemByItemId(itemId));
    }

    /**
     * 新增产品信息
     */
    @PreAuthorize("@ss.hasPermi('system:item:add')")
    @Log(title = "产品信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BasItem basItem) {
        return toAjax(basItemService.insertBasItem(basItem));
    }

    /**
     * 修改产品信息
     */
    @PreAuthorize("@ss.hasPermi('system:item:edit')")
    @Log(title = "产品信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BasItem basItem) {
        return toAjax(basItemService.updateBasItem(basItem));
    }

    /**
     * 删除产品信息
     */
    @PreAuthorize("@ss.hasPermi('system:item:remove')")
    @Log(title = "产品信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{itemIds}")
    public AjaxResult remove(@PathVariable Long[] itemIds) {
        return toAjax(basItemService.deleteBasItemByItemIds(itemIds));
    }
}
